package org.mclll.xwangplugin.mcmmoCustom.poiont.listener;

import com.gmail.nossr50.config.skills.alchemy.PotionConfig;
import com.gmail.nossr50.events.fake.FakeBrewEvent;
import com.gmail.nossr50.mcMMO;
import lombok.Data;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.BrewEvent;
import org.bukkit.inventory.ItemStack;
import org.mclll.xwangplugin.MclllCustom;
import org.mclll.xwangplugin.log.LogUtil;
import org.mclll.xwangplugin.log.LoggerFactory;
import org.mclll.xwangplugin.log.PointLog;
import org.mclll.xwangplugin.mcmmoCustom.poiont.config.ProbBrewerConfig;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author: geduo
 * @date: 2023/10/17 14:19
 */
@Data
public class PointListener implements Listener {
    private MclllCustom plugin;
    private LogUtil log;
    private PotionConfig potionConfig;

    public PointListener(MclllCustom plugin) {
        this.plugin = plugin;
        log = LoggerFactory.get(PointLog.class);
        setPointConfig();
    }
    @EventHandler
    public void onPoint(FakeBrewEvent event) {
        ItemStack inputPoint = null;
        for (ItemStack content : event.getContents().getContents()) {
            if (content != null) {
                inputPoint = content;
                break;
            }
        }
        List<ProbBrewerConfig.ProbBrewer> probList = ProbBrewerConfig.get(inputPoint, event.getContents().getIngredient().getType());
        if (probList == null || probList.isEmpty()) {
            return;
        }
        List<ItemStack> results = event.getResults();
        for (int i = 0; i < results.size(); i++) {
            ItemStack item = results.get(i);
            if (item == null) {
                continue;
            }
            double prob = Math.random() * 100;
            log.info("-----{0}概率-----{1}", i, prob);
            ItemStack output = getByProb(probList, prob);
            if(output == null) {
                continue;
            }
            results.set(i, output);
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void dependYbReprice(BrewEvent event) {
        if (event instanceof FakeBrewEvent) {
            return;
        }
        Material fuel = Optional.ofNullable(event.getContents().getIngredient())
                .map(ItemStack::getType)
                .orElse(null);
        if (Material.GLOWSTONE_DUST != fuel) {
            event.setCancelled(true);
        }
    }

    private ItemStack getByProb(List<ProbBrewerConfig.ProbBrewer> probList, double prob) {
        ItemStack ret = null;
        double start = 0;
        for (ProbBrewerConfig.ProbBrewer probBrewer : probList) {
            Double probSet = probBrewer.getProb();
            if (prob <= start + probSet) {
                ret = probBrewer.getPoint().asOne();
                break;
            }
            start += probSet;
        }
        return ret;
    }

    private void setPointConfig() {
        if (this.potionConfig != null) {
            return;
        }
        try {
            mcMMO mmo = (mcMMO) Bukkit.getPluginManager().getPlugin("mcMMO");
            ClassLoader mcmmoClassLoader = mmo.getClass().getClassLoader();
            Field field = mcmmoClassLoader.getClass().getDeclaredField("classes");
            field.setAccessible(true);
            Map<String, Class> classList = (Map<String, Class>) field.get(mcmmoClassLoader);
            for (Class aClass : classList.values()) {
                if (aClass.getName().equals("com.gmail.nossr50.config.skills.alchemy.PotionConfig")) {
                    this.potionConfig = (PotionConfig) aClass.getDeclaredMethod("getInstance").invoke(null);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
